package com.dongdong.bookstore.security.service;

import com.dongdong.bookstore.data.UserData;
import com.dongdong.bookstore.module.cms.service.UserService;
import com.dongdong.bookstore.module.ums.model.Admin;
import com.dongdong.bookstore.module.ums.service.AdminService;
import com.dongdong.bookstore.module.ums.service.UmsCacheService;
import com.dongdong.bookstore.security.model.UserCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author chiangkai
 * @date 2021-09-21
 * @email chiangkk@163.com
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        int userCategroy = Integer.parseInt(username.substring(0, 1));
        String userName = username.substring(username.indexOf(":") + 1);
        if (UserCategory.values()[userCategroy].equals(UserCategory.ADMIN)) {
            return adminService.loadUserByName(userName);
        } else if (UserCategory.values()[userCategroy].equals(UserCategory.VIP)) {
            return userService.loadUserByName(userName);
        }
        return UserData.user1();
    }
}
